Skip to content

[cli] Improve error handling and provide token location tracing#178

Merged
forfudan merged 2 commits intoclifrom
update
Mar 1, 2026
Merged

[cli] Improve error handling and provide token location tracing#178
forfudan merged 2 commits intoclifrom
update

Conversation

@forfudan
Copy link
Copy Markdown
Owner

@forfudan forfudan commented Mar 1, 2026

This PR improves the CLI calculator’s diagnostics by adding token position tracking end-to-end and rendering user-facing errors with a caret indicator in the CLI.

Changes:

  • Add position to tokenizer Token and propagate it through parsing/evaluation to produce Error at position N: ... diagnostics.
  • Update CLI entrypoint to run tokenize/parse/eval phases explicitly and render position-aware errors with a caret via a new display utility module.
  • Add/adjust CLI tests to cover new error messages and edge cases (empty expression, division by zero, negative sqrt, etc.).

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the CLI calculator’s diagnostics by adding token position tracking end-to-end and rendering user-facing errors with a caret indicator in the CLI.

Changes:

  • Add position to tokenizer Token and propagate it through parsing/evaluation to produce Error at position N: ... diagnostics.
  • Update CLI entrypoint to run tokenize/parse/eval phases explicitly and render position-aware errors with a caret via a new display utility module.
  • Add/adjust CLI tests to cover new error messages and edge cases (empty expression, division by zero, negative sqrt, etc.).

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/cli/test_tokenizer.mojo Updates empty-input behavior to expect an error.
tests/cli/test_error_handling.mojo Adds a new test suite covering malformed expressions and math edge cases, including position substrings.
src/cli/main.mojo Refactors CLI execution to tokenize/parse/evaluate explicitly and render errors with caret/colour output.
src/cli/calculator/tokenizer.mojo Adds token position tracking and emits position-aware tokenizer errors (plus empty-expression handling).
src/cli/calculator/parser.mojo Emits position-aware parse errors for commas and mismatched parentheses.
src/cli/calculator/evaluator.mojo Emits position-aware runtime/math errors (division by zero, missing operands, domain errors).
src/cli/calculator/display.mojo New module for colored stderr output and caret rendering.
src/cli/calculator/init.mojo Re-exports display helpers from the calculator package.
docs/plans/cli_calculator.md Updates the plan document with ArgMojo v0.2.0 upgrade task notes and reindexes Phase 3 items.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/cli/calculator/tokenizer.mojo Outdated
Comment on lines +166 to +173
Each token records its 0-based column position in the source
expression so that downstream stages can emit user-friendly
diagnostics that pinpoint where the problem is.

Raises:
Error: On empty/whitespace-only input, unknown identifiers, or
unexpected characters, with the column position included in
the message.
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tokenizer docstring says errors include a column position in the message, but the empty/whitespace-only case currently raises just Empty expression without the Error at position N: prefix. Either update the docstring to reflect this special case, or include position info (e.g. position 0) for empty expressions for consistency.

Copilot uses AI. Check for mistakes.
Comment thread tests/cli/test_tokenizer.mojo Outdated
"""Empty string should raise an error since Phase 3."""
var raised = False
try:
var toks = tokenize("")
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var toks = tokenize("") is never used. For consistency with the other tokenizer error tests (and to avoid unused-variable warnings if enabled), assign to _ instead of creating an unused toks binding.

Suggested change
var toks = tokenize("")
_ = tokenize("")

Copilot uses AI. Check for mistakes.
Comment thread tests/cli/test_error_handling.mojo Outdated

from calculator import evaluate
from calculator.tokenizer import tokenize
from calculator.parser import parse_to_rpn
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse_to_rpn is imported but never used in this test module. Please remove the unused import to keep the test file clean (and to avoid failing builds if unused-import checks are enabled).

Suggested change
from calculator.parser import parse_to_rpn

Copilot uses AI. Check for mistakes.
Comment thread src/cli/main.mojo Outdated
from sys import exit

from argmojo import Arg, Command
from calculator import evaluate
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evaluate is imported but no longer used (the CLI now calls tokenize/parse_to_rpn/evaluate_rpn directly). Please drop the unused from calculator import evaluate import.

Suggested change
from calculator import evaluate

Copilot uses AI. Check for mistakes.
@forfudan forfudan merged commit 85fd5c7 into cli Mar 1, 2026
1 check passed
@forfudan forfudan deleted the update branch March 1, 2026 20:17
forfudan added a commit that referenced this pull request Mar 23, 2026
This PR improves the CLI calculator’s diagnostics by adding token
position tracking end-to-end and rendering user-facing errors with a
caret indicator in the CLI.

**Changes:**
- Add `position` to tokenizer `Token` and propagate it through
parsing/evaluation to produce `Error at position N: ...` diagnostics.
- Update CLI entrypoint to run tokenize/parse/eval phases explicitly and
render position-aware errors with a caret via a new display utility
module.
- Add/adjust CLI tests to cover new error messages and edge cases (empty
expression, division by zero, negative sqrt, etc.).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants